package com.code.day03;

import com.code.utils.JDBCUtils;
import org.junit.Test;

import java.sql.*;

public class Demo03JDBC {

    @Test
    public void insert() throws SQLException {

        Connection conn=JDBCUtils.getConnection();

        String sql="insert into `t_user` (username,password) values (?,?);";
        PreparedStatement pstmt=conn.prepareStatement(sql);
        pstmt.setObject(1,"xixi");
        pstmt.setObject(2,"aasda");
        pstmt.executeUpdate();


        JDBCUtils.closeConnection(conn,pstmt,null);
    }


    @Test
    public void select() throws SQLException {
        Connection conn=JDBCUtils.getConnection();


        String sql="select * from `t_user` where username = ?;";
        PreparedStatement pstmt=conn.prepareStatement(sql);
        pstmt.setObject(1,"xixi");
        ResultSet rs=pstmt.executeQuery();
        while(rs.next()){
            int id = rs.getInt("id");
            String usr = rs.getString("username");
            String pwd = rs.getString("password");
            System.out.println(id+","+usr+","+pwd);
        }



        JDBCUtils.closeConnection(conn,pstmt,rs);
    }

}
